//
//  ZYXLoadingCell.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/12.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXLoadingCell.h"
#import "YCDownloadSession.h"
#import "ZYXClassNameModel.h"
@interface ZYXLoadingCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UILabel *spLabel;
@property (weak, nonatomic) IBOutlet UIImageView *stateImgView;
@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;
@end
@implementation ZYXLoadingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
//    self.progressView.hidden = YES;
//    self.sizeLabel.hidden = YES;

}
- (void)setItem:(YCDownloadItem *)item {
    
    _item = item;
    ZYXClassNameDerailModel *mo = [ZYXClassNameDerailModel infoWithData:item.extraData];
    self.titleLabel.text = mo.title;
    [self changeSizeLblDownloadedSize:item.downloadedSize totalSize:item.fileSize];
    [self setDownloadStatus:item.downloadStatus];
    self.spLabel.hidden = !item.enableSpeed;
    NSLog(@"savePath = %@",item.savePath);
}
- (void)setDownloadStatus:(YCDownloadStatus)status {
    
    switch (status) {
        case YCDownloadStatusWaiting:
            self.stateLabel.text = @"正在等待";
            self.stateImgView.hidden = NO;
            self.stateImgView.image = [UIImage imageNamed:@"zanting"];
            
            break;
        case YCDownloadStatusDownloading:
            self.stateLabel.text = @"正在下载";
            self.stateImgView.hidden = NO;
            self.stateImgView.image = [UIImage imageNamed:@"kaishi"];
            break;
        case YCDownloadStatusPaused:
            self.stateLabel.text = @"暂停下载";
            self.stateImgView.hidden = NO;
            self.stateImgView.image = [UIImage imageNamed:@"zanting"];
            break;
        case YCDownloadStatusFinished:
            self.stateLabel.text = @"下载成功";
            self.progressView.progress = 1;
            self.stateImgView.hidden = YES;
            break;
        case YCDownloadStatusFailed:
            self.stateLabel.text = @"下载失败";
            self.stateImgView.hidden = NO;
            self.stateImgView.image = [UIImage imageNamed:@"zanting"];
            break;
            
        default:
            break;
    }
}
- (void)downloadItemStatusChanged:(YCDownloadItem *)item {
    [self setDownloadStatus:item.downloadStatus];
}

- (void)changeSizeLblDownloadedSize:(int64_t)downloadedSize totalSize:(int64_t)totalSize {
    NSString *str =   [NSString stringWithFormat:@"%@ / %@",[YCDownloadUtils fileSizeStringFromBytes:downloadedSize], [YCDownloadUtils fileSizeStringFromBytes:totalSize]];
    self.sizeLabel.text = str;

    
    float progress = 0;
    if (totalSize != 0) {
        progress = (float)downloadedSize / totalSize;
  
    }
    self.progressView.progress = progress;
    self.spLabel.text =[NSString stringWithFormat:@"%.0f%%",progress*100];
}
+(NSString *)notRounding:(double)dValue afterPoint:(int)position{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:dValue];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    
    return [NSString stringWithFormat:@"%@",roundedOunces];
    
}
- (void)downloadItem:(YCDownloadItem *)item downloadedSize:(int64_t)downloadedSize totalSize:(int64_t)totalSize {
    
    [self changeSizeLblDownloadedSize:downloadedSize totalSize:totalSize];
}
- (void)downloadItem:(YCDownloadItem *)item speed:(NSUInteger)speed speedDesc:(NSString *)speedDesc {
//    self.spLabel.text = speedDesc;
}
@end
